/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Dao;

import BackEnd.Exceptions.*;
import com.mysql.jdbc.*;
import com.mysql.jdbc.ResultSet;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Types;
//import java.util.logging.Level;
//import java.util.logging.Logger;

/**
 *
 * @author Ivan
 */
public class SesionDao extends DaoBase{

    @Override
    public ValueObjectBase create(ValueObjectBase value) throws CreateRegisterException {
        if(value.getClass()==SesionValueObject.class)
        {
            SesionValueObject vo=(SesionValueObject)value;
            try {
            Connection con=super.getConection("dlc");
            String sentencia="call pa_registrarSesion(?,?,?,?)";
            java.sql.CallableStatement wcall=con.prepareCall(sentencia);
            wcall.setInt(1, vo.getIdUsuario());
            wcall.setString(2, "@a");
            wcall.setString(3, "@var");
            wcall.setString(4, "@var1");
            wcall.registerOutParameter(2, Types.DATE);
            wcall.registerOutParameter(3, Types.TIME);
            wcall.registerOutParameter(4, Types.INTEGER);
            wcall.execute();
            vo.setFechaInicio(wcall.getDate(2));
            vo.setHoraInicio(wcall.getTime(3));
            vo.setId(wcall.getInt(4));
            wcall.close();
            con.close();
        } catch (SQLException ex) {
            throw new CreateRegisterException();
        }
             return vo;
        }
        else
            throw new CreateRegisterException();
    }

    @Override
    public void update(ValueObjectBase value) throws UpdateRegisterException {
        if(value.getClass()==SesionValueObject.class)
        {
            SesionValueObject vo=(SesionValueObject)value;
                Connection con;
            try {
                con = super.getConection("dlc");

                String sentencia="call pa_registrarFinSesion(?,?,?,?)";
                java.sql.CallableStatement wcall=con.prepareCall(sentencia);
                wcall.setInt(1, vo.getId());
                wcall.setString(2, vo.getModoSalida());
                wcall.setString(3,"@a");
                wcall.setString(4, "@a1");
                wcall.registerOutParameter(3, Types.DATE);
                wcall.registerOutParameter(4, Types.TIME);
                wcall.execute();
                vo.setFechaFin(wcall.getDate(3));
                vo.setHoraFin(wcall.getTime(4));
                wcall.close();
                con.close();
                } catch (SQLException ex) {
                throw new UpdateRegisterException();
            }
        }
    }

    @Override
    public ValueObjectBase retrive(PkBase pk) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void delete(PkBase pk) throws DeleteRegisterException{
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ValueObjectBase[] listarTodos() throws NotExistRegisterException {
        return this.mostrarConsulta("select * from sesion");
    }

    @Override
    public ValueObjectBase[] mostrarConsulta(String consulta) throws NotExistRegisterException {
        SesionValueObject[] vec=null;
            try {
                Connection con = super.getConection("dlc");
                Statement st=(Statement)con.createStatement();
                st.execute(consulta);
                ResultSet res=(ResultSet) st.getResultSet();
                res.last();
                vec = new SesionValueObject[res.getRow()];
                res.first();
                vec[0]=this.leerVo(res);
                while(res.next())
                {
                    vec[res.getRow()-1]=this.leerVo(res);
                }
                res.close();
                con.close();

            } catch (SQLException ex) {
                throw new NotExistRegisterException();
            }
        return vec;
    }

    @Override
    public ValueObjectBase traer(PkBase pk) throws NotExistRegisterException {
        if(pk.getClass() == PkId.class)
        {
            PkId id=(PkId)pk;
            SesionValueObject vo = new SesionValueObject();
            try {
                Connection con = super.getConection("dlc");
                String sentencia = "select * from sesion where id="+id.getId();
                Statement st=(Statement)con.createStatement();
                st.execute(sentencia);
                ResultSet res=(ResultSet) st.getResultSet();
                res.next();
                vo=this.leerVo(res);
                res.close();
                con.close();

            } catch (SQLException ex) {
                throw new NotExistRegisterException();
            }

            return vo;
        }
        else
            return null;
    }
    private SesionValueObject leerVo(ResultSet res) throws SQLException
    {
        SesionValueObject vo=new SesionValueObject();
        vo.setId(res.getInt("id"));
        vo.setIdUsuario(res.getInt("idUsuario"));
        vo.setFechaInicio(res.getDate("fechaInicio"));
        vo.setFechaFin(res.getDate("fechaFin"));
        vo.setHoraInicio(res.getTime("horaInicio"));
        vo.setHoraFin(res.getTime("horaFin"));
        vo.setModoSalida(res.getString("modoSalida"));
        return vo;
    }
}
